package main

import (
	"GORM_Demo04/part07/TestStruct"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

/**
多对多添加数据
	--索引在哪个结构体中创建就对哪个结构体操作


*/

func main() {
	db, err := gorm.Open("mysql", "root:123456@tcp(192.168.11.189:3306)/lztest?charset=utf8&parseTime=True&loc=Local")

	if err != nil {
		panic(err)
	}
	defer db.Close()

	//需要先创建两张表
	db.CreateTable(&TestStruct.Student{})
	db.CreateTable(&TestStruct.Course{})

	student1 := TestStruct.Student{
		SId:  1,
		SNo:  1001,
		Name: "张三",
		Sex:  "男",
		Age:  12,
		Course: []TestStruct.Course{
			{
				CName:       "语文",
				TeacherName: "王老师",
				Room:        "403教师",
			},
			{
				CName:       "数学",
				TeacherName: "李老师",
				Room:        "303教师",
			},
		},
	}

	student2 := TestStruct.Student{
		SId:  2,
		SNo:  1002,
		Name: "陈学生",
		Sex:  "nv",
		Age:  15,
		Course: []TestStruct.Course{
			{
				CName:       "语文",
				TeacherName: "王老师",
				Room:        "403教师",
			},
			{
				CName:       "数学",
				TeacherName: "李老师",
				Room:        "303教师",
			},
		},
	}

	db.Create(student1)
	db.Create(student2)

}
